<?php
declare (strict_types = 1);
namespace app\admin\controller;

use think\facade\Session;
use think\Request;
use think\facade\View;
use think\exception\ValidateException;

use app\admin\validate\StarValidate;

use app\admin\model\Star;

class StarController extends BaseController
{
    // 渲染星级列表页面
    public function star_list()
    {   
        return view();
    }

    // 渲染星级编辑页面
    public function star_edit()
    {   
        $id = input('id');
        $starModel = new Star();

        $info = $starModel->getFind(['id' => $id], 0)['data'];

        View::assign('info', $info);
        return view();
    }

    // 获取星级列表
    public function star_data()
    {   
        $starModel = new Star();
        $list = $starModel->getList([], 0);

        if($list['code'] != 'ok'){
            $this->error_back('暂无数据~');
        }

        $this->success_back('ok', $list['data']);
    }

    // 星级信息修改
    public function setStarEdit()
    {   
        $starModel = new Star();
        $data = input('post.');

        // 验证数据
        try{
            validate(StarValidate::class)->scene('edit')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }
        
        $flag = $starModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('修改成功');
    }
}
